package com.fh.controller.wechatapp;

import com.fh.controller.base.BaseController;
import com.fh.dto.TranListResponse;
import com.fh.dto.TranListReuest;
import com.fh.dto.TranRegisterRequest;
import com.fh.dto.TranRegisterResponse;
import com.fh.entity.HttpResult;
import com.fh.entity.Page;
import com.fh.entity.airsys.MerchatUserInfo;
import com.fh.service.airsys.registeronlinetran.RegisteronLineTranService;
import com.fh.service.airsys.transactiondetails.TransactionDetailsService;
import com.fh.service.airsys.userrummery.UserRummeryService;
import com.fh.service.system.user.UserService;
import com.fh.util.PageData;
import org.apache.shiro.crypto.hash.SimpleHash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by tugou on 2017/12/3.
 */
@Controller @RequestMapping("/merchantApi")
public class MerchantApiController extends BaseController{

    @Autowired
    UserService userService;
    @Autowired
    UserRummeryService userRummeryService;
    @Autowired
    TransactionDetailsService transactionDetailsService;
    @Autowired
    private RegisteronLineTranService registeronlinetranService;

    @RequestMapping("getUserInfoByOpenId") @ResponseBody
    public HttpResult getUserInfoByOpenId(String openId){
        if(openId == null || "".equals(openId)){
            return HttpResult.ERROR("10001","OPENID不能为空");
        }
        PageData pd = new PageData();
        pd.put("OPEN_ID",openId);
        try {
            pd = userService.getUserByNameAndPwd(pd);
            if(pd == null){
                return HttpResult.ERROR("10002","查询结果为空");
            }
            MerchatUserInfo info = new MerchatUserInfo(pd);
            return HttpResult.OK(info);
        }catch (Exception e){
            logBefore(logger,e.getMessage());
            return HttpResult.ERROR("10000","系统异常");
        }
    }

    @RequestMapping("getUserInfoByPwd") @ResponseBody
    public HttpResult getUserInfoByPwd(String pwd,String userName,String openId){
        if(pwd == null || "".equals(pwd)){
            return HttpResult.ERROR("10003","密码不能为空");
        }
        if(userName == null || "".equals(userName)){
            return HttpResult.ERROR("10004","用户名不能为空");
        }
        if(openId == null || "".equals(openId)){
            return HttpResult.ERROR("10001","OPENID不能为空");
        }
        pwd = new SimpleHash("SHA-1", userName, pwd).toString();	//密码加密
        PageData pd = new PageData();
        pd.put("USERNAME",userName);
        pd.put("PASSWORD",pwd);
        try {
            pd = userService.getUserByNameAndPwd(pd);
            if(pd == null){
                return HttpResult.ERROR("10002","查询结果为空");
            }
            MerchatUserInfo info = new MerchatUserInfo(pd);
            if(!openId.equals(info.getOpenId())){
                PageData mechInfo = new PageData();
                mechInfo.put("USER_ID",pd.getString("USER_ID"));
                mechInfo.put("AIRLINERUMMERY_ID",pd.getString("AIRLINERUMMERY_ID"));
                mechInfo.put("OPEN_ID",openId);
                userRummeryService.edit(mechInfo);
            }
            return HttpResult.OK(info);
        } catch (Exception e) {
            logBefore(logger,e.getMessage());
            return HttpResult.ERROR("10000","系统异常");
        }
    }

    @RequestMapping("loginOut") @ResponseBody
    public HttpResult getUserInfoByPwd(String openId){
        if(openId == null || "".equals(openId)){
            return HttpResult.ERROR("10001","OPENID不能为空");
        }
        PageData pd = new PageData();
        pd.put("OPEN_ID",openId);
        try {
            pd = userService.getUserByNameAndPwd(pd);
            if(pd != null){
                PageData mechInfo = new PageData();
                mechInfo.put("USER_ID",pd.getString("USER_ID"));
                mechInfo.put("AIRLINERUMMERY_ID",pd.getString("AIRLINERUMMERY_ID"));
                mechInfo.put("OPEN_ID",null);
                userRummeryService.edit(mechInfo);
            }
            return HttpResult.OK(null);
        }catch (Exception e){
            logBefore(logger,e.getMessage());
            return HttpResult.ERROR("10000","系统异常");
        }
    }

    @RequestMapping("getTranInfoListByMrchId") @ResponseBody
    public HttpResult getTranInfoListByMrchId(TranListReuest tranListReuest){
        if (tranListReuest.getMrchId() == null || "".equals(tranListReuest.getMrchId())){
            return HttpResult.ERROR("10005","商户id不能为空");
        }
        Page<TranListResponse> page = new Page<TranListResponse>();
        if(tranListReuest.getCount()==null || tranListReuest.getCount() == 0){
            page.setShowCount(10);
        }
        else
            page.setShowCount(tranListReuest.getCount());
        if(tranListReuest.getPageNo() == null || tranListReuest.getPageNo() == 0){
            tranListReuest.setPageNo(1);
        }
        page.setCurrentPage(tranListReuest.getPageNo());
        PageData pd = new PageData();
        pd.put("RI_CODE",tranListReuest.getMrchId());
        pd.put("TRAN_TYPE_LIST", "'1011','1014'");
        try{
            List<PageData> varList = transactionDetailsService.list(page);
            List<TranListResponse> list = TranListResponse.getList(varList);
            com.fh.dto.Page<TranListResponse> result = new com.fh.dto.Page<TranListResponse>(page,list);
            return HttpResult.OK(result);
        }
        catch (Exception e){
            logBefore(logger,e.getMessage());
            return HttpResult.ERROR("10000","系统异常");
        }
    }

    @RequestMapping("getRegisteronLineByMrchId") @ResponseBody
    public HttpResult getRegisteronLineByMrchId(TranRegisterRequest request){
        Page<TranListResponse> page = new Page<TranListResponse>();
        if(request.getCount()==null || request.getCount() == 0){
            page.setShowCount(10);
        }
        else
            page.setShowCount(request.getCount());
        if(request.getPageNo() == null || request.getPageNo() == 0){
            page.setCurrentPage(1);
        }
        else
            page.setCurrentPage(request.getPageNo());
        PageData pd = new PageData();
        try {
            pd.put("AIRLINERUMMERY_ID",request.getMrchId());
            pd.put("RL_STATE",request.getState());
            pd.put("RL_TYPE",request.getType());
            page.setPd(pd);
            List<PageData>	varList = registeronlinetranService.list(page);	//列出RegisteronLineTran列表
            List<TranRegisterResponse> list = TranRegisterResponse.getList(varList);
            com.fh.dto.Page<TranRegisterResponse> result = new com.fh.dto.Page<TranRegisterResponse>(page,list);
            return HttpResult.OK(result);
        } catch (Exception e) {
            return HttpResult.ERROR("55555",e.getMessage());
        }
    }
}
